/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package social.p2p.controller;

import com.jpanative.ConnectionProvider;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;

/**
 *
 * @author cadmilo
 */
public class JDBCConnectionProvider implements ConnectionProvider {

    private String clazz;
    private String url;
    private String usr;
    private String pwd;
    public static boolean test;
    public final static String DBR = Math.random() + "";

    @Override
    public void configure(HashMap<String, String> properties) {
        url = properties.get("javax.persistence.jdbc.url");
        usr = properties.get("javax.persistence.jdbc.user");
        pwd = properties.get("javax.persistence.jdbc.password");
    }

    @Override
    public Connection getConnection() throws SQLException {
        if (!test) {
            return DriverManager.getConnection(url, usr, pwd);
        } else {
            return DriverManager.getConnection(url + DBR, usr, pwd);
        }
    }

    @Override
    public void closeConnection(Connection conn) throws SQLException {
        conn.close();
    }
}
